---
description: "Reference for Nitric's v0 Go library - Publish new events to the topic."
---

# Go - Topic.Publish()

Publish an event (push based message) to a topic.

```go
import (
  "context"
  "fmt"

  "github.com/nitrictech/go-sdk/api/events"
  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  updates, err := nitric.NewTopic("updates").With(nitric.TopicPublishing)
  if err != nil {
    fmt.Println(err)

    return
  }

  updates.Publish(context.TODO(), &events.Event{
    Payload: map[string]interface{}{
      "something": "amazing happened",
    },
  })

  nitric.Run()
}
```

## Parameters

<Properties>
  <Property name="ctx" required type="context">
    The context of the call, used for tracing.
  </Property>
  <Property name="event" required type="Event">
    The event to publish to the topic.
    <Properties nested>
      <Property name="ID" type="string">
        Unique ID to apply to the event.
      </Property>
      <Property name="Payload" required type="map[string]interface{}">
        Payload to send with the event.
      </Property>
      <Property name="PayloadType" type="string">
        A hint to the type of payload supplied.
      </Property>
    </Properties>
  </Property>
  <Property name="opts" type="PublishOption">
    Optional function to send a message with a delay.
  </Property>
</Properties>

## Examples

### Publish a message

Publishing messages to a topic will push a copy of the message to each of the topic's subscribers. By default, delivery occurs without a delay.

```go
import (
  "context"
  "fmt"

  "github.com/nitrictech/go-sdk/api/events"
  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  updates, err := nitric.NewTopic("updates").With(nitric.TopicPublishing)
  if err != nil {
    fmt.Println(err)

    return
  }

  err := updates.Publish(context.TODO(), &events.Event{
    Payload: map[string]interface{}{
      "something": "amazing happened",
    },
  })
  nitric.Run()
}
```

### Delaying message delivery

You can delay the delivery of messages sent to a topic. The current maximum delay is 7 days (604800 seconds).

```go
import (
  "context"
  "fmt"
  "time"

  "github.com/nitrictech/go-sdk/api/events"
  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  updates, err := nitric.NewTopic("updates").With(nitric.TopicPublishing)
  if err != nil {
    fmt.Println(err)

    return
  }

  updates.Publish(context.TODO(), &events.Event{
    Payload: map[string]interface{}{
      "something": "amazing happened",
    },
  }, events.WithDelay(time.Hour))

  nitric.Run()
}
```

### Notes

- If an id is not supplied with an event a UUID(v4) will be generated for you.
- A function may subscribe to OR publish to a topic but not both.
